<template>
  <div>
    <router-link to="/add">新增</router-link>
    <table>
      <tr>
        <th>id</th>
        <th>userName</th>
        <th>userAge</th>
        <th>userAddress</th>
        <th>编辑</th>
        <th>删除</th>
      </tr>
      <tr v-for="item in userData" :key="item.id">
        <td>{{item.id}}</td>
        <td>{{item.userName}}</td>
        <td>{{item.userAge}}</td>
        <td>{{item.userAddress}}</td>
      <td>
       <router-link :to="{path:'edit',query:{id:item.id}}">编辑</router-link>
      </td>
      <td @click="deleted(item.id)">删除</td>
      </tr>
    </table>
  </div>
</template>

<script>
import axios from '@/service/httpService'
export default {
  data () {
    return {
      userData: ''
    }
  },
  methods: {
    getUserData () {
      axios
        .get('/users')
        .then(response => {
          this.userData = response.data
        })
        .catch(error => {
          console.log(error)
        })
    },
    deleted (id) {
      var deleteConfirm = confirm('是否删除')
      if (deleteConfirm) {
        axios.delete(`/deleteUser/${id}`).then(response => {
          this.getUserData()
        })
      }
    }
  },
  created () {
    this.getUserData()
  }
}
</script>
